<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Chapter 7: Classes | A君的秘密基地</title><meta name="keywords" content="C++ Primer"><meta name="author" content="astralcon"><meta name="copyright" content="astralcon"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Exercises Section 7.1.1Ex7.1123456789101112131415161718192021222324252627282930313233343536#include&lt;iostream&gt;#include&lt;string&gt;struct Sales_data &amp;#123;    std::string bookNo;    unsigned uni">
<meta property="og:type" content="article">
<meta property="og:title" content="Chapter 7: Classes">
<meta property="og:url" content="https://astralcon.gitee.io/blog/2022/05/14/Chapter-7-Classes/index.html">
<meta property="og:site_name" content="A君的秘密基地">
<meta property="og:description" content="Exercises Section 7.1.1Ex7.1123456789101112131415161718192021222324252627282930313233343536#include&lt;iostream&gt;#include&lt;string&gt;struct Sales_data &amp;#123;    std::string bookNo;    unsigned uni">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="">
<meta property="article:published_time" content="2022-05-14T06:26:03.000Z">
<meta property="article:modified_time" content="2022-05-14T06:56:09.071Z">
<meta property="article:author" content="astralcon">
<meta property="article:tag" content="C++ Primer">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content=""><link rel="shortcut icon" href="/blog/img/avatar.jpg"><link rel="canonical" href="https://astralcon.gitee.io/blog/2022/05/14/Chapter-7-Classes/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/blog/',
  algolia: {"appId":"YUB0ZFWBBL","apiKey":"78d0f1fae31a1df3e0cb573725c55558","indexName":"blog","hits":{"per_page":6},"languages":{"input_placeholder":"搜索文章","hits_empty":"找不到您查询的内容：${query}","hits_stats":"找到 ${hits} 条结果，用时 ${time} 毫秒"}},
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Chapter 7: Classes',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-05-14 14:56:09'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.2.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/blog/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">27</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">3</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首頁</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-folder-open"></i><span> 目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/game/"><i class="fa-fw fas fa-gamepad"></i><span> 游戏</span></a></li><li><a class="site-page child" href="/blog/book/"><i class="fa-fw fas fa-book"></i><span> 书籍</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blog/">A君的秘密基地</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首頁</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-folder-open"></i><span> 目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/game/"><i class="fa-fw fas fa-gamepad"></i><span> 游戏</span></a></li><li><a class="site-page child" href="/blog/book/"><i class="fa-fw fas fa-book"></i><span> 书籍</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Chapter 7: Classes</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-05-14T06:26:03.000Z" title="发表于 2022-05-14 14:26:03">2022-05-14</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-05-14T06:56:09.071Z" title="更新于 2022-05-14 14:56:09">2022-05-14</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/C/">C++</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-commentcount"><i class="far fa-comments fa-fw post-meta-icon"></i><span class="post-meta-label">评论数:</span><a href="/blog/2022/05/14/Chapter-7-Classes/#post-comment" itemprop="discussionUrl"><span class="valine-comment-count" data-xid="/blog/2022/05/14/Chapter-7-Classes/" itemprop="commentCount"></span></a></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Exercises-Section-7-1-1"><a href="#Exercises-Section-7-1-1" class="headerlink" title="Exercises Section 7.1.1"></a>Exercises Section 7.1.1</h1><h2 id="Ex7-1"><a href="#Ex7-1" class="headerlink" title="Ex7.1"></a>Ex7.1</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Sales_data</span> </span><br><span class="line">&#123;</span><br><span class="line">    std::string bookNo;</span><br><span class="line">    <span class="type">unsigned</span> units_sold = <span class="number">0</span>;</span><br><span class="line">    <span class="type">double</span> revenue = <span class="number">0.0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Sales_data total;</span><br><span class="line">    <span class="keyword">if</span> (std::cin &gt;&gt; total.bookNo &gt;&gt; total.units_sold &gt;&gt; total.revenue)</span><br><span class="line">    &#123;</span><br><span class="line">        Sales_data trans;</span><br><span class="line">        <span class="keyword">while</span> (std::cin &gt;&gt; trans.bookNo &gt;&gt; trans.units_sold &gt;&gt; trans.revenue)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">if</span> (total.bookNo == trans.bookNo)</span><br><span class="line">                total.units_sold += trans.units_sold;</span><br><span class="line">            <span class="keyword">else</span> </span><br><span class="line">            &#123;</span><br><span class="line">                std::cout &lt;&lt; total.bookNo &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; total.units_sold &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; total.revenue &lt;&lt; std::endl;</span><br><span class="line">                total = trans;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        std::cout &lt;&lt; total.bookNo &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; total.units_sold &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; total.revenue &lt;&lt; std::endl;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> </span><br><span class="line">    &#123;</span><br><span class="line">        std::cerr &lt;&lt; <span class="string">&quot;No data?!&quot;</span> &lt;&lt; std::endl;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-7-1-2"><a href="#Exercises-Section-7-1-2" class="headerlink" title="Exercises Section 7.1.2"></a>Exercises Section 7.1.2</h1><h2 id="Ex7-2"><a href="#Ex7-2" class="headerlink" title="Ex7.2"></a>Ex7.2</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">Sales_data</span> </span><br><span class="line">&#123;</span><br><span class="line">    <span class="function">std::string <span class="title">isbn</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> bookNo; &#125;</span><br><span class="line">    <span class="function">Sales_data&amp; <span class="title">combine</span><span class="params">( <span class="type">const</span> Sales_data&amp;)</span></span>;</span><br><span class="line">    std::string bookNo;</span><br><span class="line">    <span class="type">unsigned</span> units_sold = <span class="number">0</span>;</span><br><span class="line">    <span class="type">double</span> revenue = <span class="number">0.0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function">Sales_data&amp; <span class="title">Sales_data::combine</span><span class="params">(<span class="type">const</span> Sales_data &amp;rhs)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    units_sold += rhs.units_sold;</span><br><span class="line">    revenue += rhs.revenue;</span><br><span class="line">    <span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-3"><a href="#Ex7-3" class="headerlink" title="Ex7.3"></a>Ex7.3</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Sales_data</span> </span><br><span class="line">&#123;</span><br><span class="line">    <span class="function">std::string <span class="title">isbn</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> bookNo; &#125;</span><br><span class="line">    <span class="function">Sales_data&amp; <span class="title">combine</span><span class="params">( <span class="type">const</span> Sales_data&amp;)</span></span>;</span><br><span class="line">    std::string bookNo;</span><br><span class="line">    <span class="type">unsigned</span> units_sold = <span class="number">0</span>;</span><br><span class="line">    <span class="type">double</span> revenue = <span class="number">0.0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function">Sales_data&amp; <span class="title">Sales_data::combine</span><span class="params">(<span class="type">const</span> Sales_data &amp;rhs)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    units_sold += rhs.units_sold;</span><br><span class="line">    revenue += rhs.revenue;</span><br><span class="line">    <span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Sales_data total;</span><br><span class="line">    <span class="keyword">if</span> (std::cin &gt;&gt; total.bookNo &gt;&gt; total.units_sold &gt;&gt; total.revenue)</span><br><span class="line">    &#123;</span><br><span class="line">        Sales_data trans;</span><br><span class="line">        <span class="keyword">while</span> (std::cin &gt;&gt; trans.bookNo &gt;&gt; trans.units_sold &gt;&gt; trans.revenue)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">if</span> (total.<span class="built_in">isbn</span>() == trans.<span class="built_in">isbn</span>())</span><br><span class="line">                total.<span class="built_in">combine</span>(trans);</span><br><span class="line">            <span class="keyword">else</span> </span><br><span class="line">            &#123;</span><br><span class="line">                std::cout &lt;&lt; total.bookNo &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; total.units_sold &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; total.revenue &lt;&lt; std::endl;</span><br><span class="line">                total = trans;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        std::cout &lt;&lt; total.bookNo &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; total.units_sold &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; total.revenue &lt;&lt; std::endl;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> </span><br><span class="line">    &#123;</span><br><span class="line">        std::cerr &lt;&lt; <span class="string">&quot;No data?!&quot;</span> &lt;&lt; std::endl;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<h2 id="Ex7-4"><a href="#Ex7-4" class="headerlink" title="Ex7.4"></a>Ex7.4</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">Person</span></span><br><span class="line">&#123;</span><br><span class="line">    std::string name;</span><br><span class="line">    std::string address;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-5"><a href="#Ex7-5" class="headerlink" title="Ex7.5"></a>Ex7.5</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">Person</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="function">std::string <span class="title">getName</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> name; &#125;</span><br><span class="line">    <span class="function">std::string <span class="title">getAddr</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> address; &#125;</span><br><span class="line">    std::string name;</span><br><span class="line">    std::string address;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-7-1-3"><a href="#Exercises-Section-7-1-3" class="headerlink" title="Exercises Section 7.1.3"></a>Exercises Section 7.1.3</h1><h2 id="Ex7-6"><a href="#Ex7-6" class="headerlink" title="Ex7.6"></a>Ex7.6</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">std::istream &amp;<span class="title">read</span><span class="params">(istream &amp;is, Sales_data &amp;item)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">double</span> price = <span class="number">0.0</span>;</span><br><span class="line">    is &gt;&gt; item.bookNo &gt;&gt; item.units_sold &gt;&gt; price;</span><br><span class="line">    item.revenue = price * item.units_sold;</span><br><span class="line">    <span class="keyword">return</span> is;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">std::ostream &amp;<span class="title">print</span><span class="params">(ostream &amp;os, <span class="type">const</span> Sales_data &amp;item)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    os &lt;&lt; item.<span class="built_in">isbn</span>() &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; item.units_sold &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; item.revenue;</span><br><span class="line">    <span class="keyword">return</span> os;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">Sales_data <span class="title">add</span><span class="params">(<span class="type">const</span> Sales_data &amp;lhs, <span class="type">const</span> Sales_data &amp;rhs)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Sales_data sum = lhs;</span><br><span class="line">    sum.<span class="built_in">combine</span>(rhs);</span><br><span class="line">    <span class="keyword">return</span> sum;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-7"><a href="#Ex7-7" class="headerlink" title="Ex7.7"></a>Ex7.7</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Sales_data</span> </span><br><span class="line">&#123;</span><br><span class="line">    <span class="function">string <span class="title">isbn</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> bookNo; &#125;</span><br><span class="line">    <span class="function">Sales_data &amp;<span class="title">combine</span><span class="params">( <span class="type">const</span> Sales_data &amp;)</span></span>;</span><br><span class="line">    string bookNo;</span><br><span class="line">    <span class="type">unsigned</span> units_sold = <span class="number">0</span>;</span><br><span class="line">    <span class="type">double</span> revenue = <span class="number">0.0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function">Sales_data&amp; <span class="title">Sales_data::combine</span><span class="params">(<span class="type">const</span> Sales_data &amp;rhs)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    units_sold += rhs.units_sold;</span><br><span class="line">    revenue += rhs.revenue;</span><br><span class="line">    <span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">istream &amp;<span class="title">read</span><span class="params">(istream &amp;is, Sales_data &amp;item)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">double</span> price = <span class="number">0.0</span>;</span><br><span class="line">    is &gt;&gt; item.bookNo &gt;&gt; item.units_sold &gt;&gt; price;</span><br><span class="line">    item.revenue = price * item.units_sold;</span><br><span class="line">    <span class="keyword">return</span> is;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">ostream &amp;<span class="title">print</span><span class="params">(ostream &amp;os, <span class="type">const</span> Sales_data &amp;item)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    os &lt;&lt; item.<span class="built_in">isbn</span>() &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; item.units_sold &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; item.revenue;</span><br><span class="line">    <span class="keyword">return</span> os;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">Sales_data <span class="title">add</span><span class="params">(<span class="type">const</span> Sales_data &amp;lhs, <span class="type">const</span> Sales_data &amp;rhs)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Sales_data sum = lhs;</span><br><span class="line">    sum.<span class="built_in">combine</span>(rhs);</span><br><span class="line">    <span class="keyword">return</span> sum;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Sales_data total;</span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">read</span>(cin, total))</span><br><span class="line">    &#123;</span><br><span class="line">        Sales_data trans;</span><br><span class="line">        <span class="keyword">while</span> (<span class="built_in">read</span>(cin, trans))</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">if</span> (total.<span class="built_in">isbn</span>() == trans.<span class="built_in">isbn</span>())</span><br><span class="line">                total = <span class="built_in">add</span>(total, trans);</span><br><span class="line">            <span class="keyword">else</span> </span><br><span class="line">            &#123;</span><br><span class="line">                <span class="built_in">print</span>(cout, total);</span><br><span class="line">                cout &lt;&lt; endl;</span><br><span class="line">                total = trans;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">print</span>(cout, total);</span><br><span class="line">        cout &lt;&lt; endl;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> </span><br><span class="line">    &#123;</span><br><span class="line">        std::cerr &lt;&lt; <span class="string">&quot;No data?!&quot;</span> &lt;&lt; std::endl;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-8"><a href="#Ex7-8" class="headerlink" title="Ex7.8"></a>Ex7.8</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">因为 <span class="built_in">read</span>() 函数需要传入 cin，改变了定义的变量值；</span><br><span class="line"><span class="built_in">print</span>() 函数不需要改变变量值故而定义为 <span class="type">const</span></span><br></pre></td></tr></table></figure>

<h2 id="Ex7-9"><a href="#Ex7-9" class="headerlink" title="Ex7.9"></a>Ex7.9</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">Person</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="function">std::string <span class="title">getName</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> name; &#125;</span><br><span class="line">    <span class="function">std::string <span class="title">getAddr</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> address; &#125;</span><br><span class="line">    std::string name;</span><br><span class="line">    std::string address;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function">std::istream &amp;<span class="title">read</span><span class="params">(istream &amp;is, Person &amp;item)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    is &gt;&gt; item.name &gt;&gt; item.address;</span><br><span class="line">    <span class="keyword">return</span> is;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">std::ostream &amp;<span class="title">print</span><span class="params">(ostream &amp;os, <span class="type">const</span> Person &amp;item)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    os &lt;&lt; item.<span class="built_in">getName</span>() &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; item.<span class="built_in">getAddr</span>();</span><br><span class="line">    <span class="keyword">return</span> os;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-10"><a href="#Ex7-10" class="headerlink" title="Ex7.10"></a>Ex7.10</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (<span class="built_in">read</span>(<span class="built_in">read</span>(cin, data1), data2)	<span class="comment">// 判断 data1 和 data2 是否都有数据输入 </span></span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-7-1-4"><a href="#Exercises-Section-7-1-4" class="headerlink" title="Exercises Section 7.1.4"></a>Exercises Section 7.1.4</h1><h2 id="Ex7-11-7-12-7-13-7-14"><a href="#Ex7-11-7-12-7-13-7-14" class="headerlink" title="Ex7.11 7.12 7.13 7.14"></a>Ex7.11 7.12 7.13 7.14</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Sales_data</span> </span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">Sales_data</span>() = <span class="keyword">default</span>;</span><br><span class="line">    <span class="built_in">Sales_data</span>(<span class="type">const</span> std::string &amp;s) : <span class="built_in">bookNo</span>(s) &#123;&#125;</span><br><span class="line">    <span class="built_in">Sales_data</span>(<span class="type">const</span> std::string &amp;s, <span class="type">unsigned</span> n, <span class="type">double</span> p) : <span class="built_in">bookNo</span>(s), <span class="built_in">units_sold</span>(n), <span class="built_in">revenue</span>(p * n) &#123;&#125;</span><br><span class="line">    <span class="built_in">Sales_data</span>(std::istream &amp;);</span><br><span class="line">    <span class="function">std::string <span class="title">isbn</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> bookNo; &#125;</span><br><span class="line">    <span class="function">Sales_data &amp;<span class="title">combine</span><span class="params">( <span class="type">const</span> Sales_data &amp;)</span></span>;</span><br><span class="line">    std::string bookNo;</span><br><span class="line">    <span class="type">unsigned</span> units_sold = <span class="number">0</span>;</span><br><span class="line">    <span class="type">double</span> revenue = <span class="number">0.0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function">Sales_data&amp; <span class="title">Sales_data::combine</span><span class="params">(<span class="type">const</span> Sales_data &amp;rhs)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    units_sold += rhs.units_sold;</span><br><span class="line">    revenue += rhs.revenue;</span><br><span class="line">    <span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">istream &amp;<span class="title">read</span><span class="params">(istream &amp;is, Sales_data &amp;item)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">double</span> price = <span class="number">0.0</span>;</span><br><span class="line">    is &gt;&gt; item.bookNo &gt;&gt; item.units_sold &gt;&gt; price;</span><br><span class="line">    item.revenue = price * item.units_sold;</span><br><span class="line">    <span class="keyword">return</span> is;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">ostream &amp;<span class="title">print</span><span class="params">(ostream &amp;os, <span class="type">const</span> Sales_data &amp;item)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    os &lt;&lt; item.<span class="built_in">isbn</span>() &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; item.units_sold &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; item.revenue &lt;&lt; endl;</span><br><span class="line">    <span class="keyword">return</span> os;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="function">Sales_data <span class="title">s1</span><span class="params">(<span class="string">&quot;&quot;</span>, <span class="number">0.0</span>, <span class="number">0.0</span>)</span></span>;</span><br><span class="line">    <span class="function">Sales_data <span class="title">s2</span><span class="params">(<span class="string">&quot;0-201-78134-X&quot;</span>)</span></span>;</span><br><span class="line">    <span class="function">Sales_data <span class="title">s3</span><span class="params">(<span class="string">&quot;0-201-78134-X&quot;</span>, <span class="number">10</span>, <span class="number">0.2</span>)</span></span>;</span><br><span class="line">    Sales_data s4;</span><br><span class="line">    <span class="built_in">read</span>(cin, s4);</span><br><span class="line">    <span class="built_in">print</span>(cout, s1);</span><br><span class="line">    <span class="built_in">print</span>(cout, s2);</span><br><span class="line">    <span class="built_in">print</span>(cout, s3);</span><br><span class="line">    <span class="built_in">print</span>(cout, s4);</span><br><span class="line">    <span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-15"><a href="#Ex7-15" class="headerlink" title="Ex7.15"></a>Ex7.15</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">Person</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">Person</span>() = <span class="keyword">default</span>;</span><br><span class="line">    <span class="built_in">Person</span>(<span class="type">const</span> std::string &amp;s) : <span class="built_in">name</span>(s) &#123;&#125;</span><br><span class="line">    <span class="built_in">Person</span>(<span class="type">const</span> std::string &amp;s, <span class="type">const</span> std::string &amp;a) : <span class="built_in">name</span>(s), <span class="built_in">address</span>(a) &#123;&#125;</span><br><span class="line">    <span class="function">string <span class="title">getName</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> name; &#125;</span><br><span class="line">    <span class="function">string <span class="title">getAddr</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> address; &#125;</span><br><span class="line">    std::string name;</span><br><span class="line">    std::string address;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-7-2"><a href="#Exercises-Section-7-2" class="headerlink" title="Exercises Section 7.2"></a>Exercises Section 7.2</h1><h2 id="Ex7-16"><a href="#Ex7-16" class="headerlink" title="Ex7.16"></a>Ex7.16</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span>：后面应该跟函数和方法</span><br><span class="line"><span class="keyword">private</span>：后面应该跟定义的变量</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-17"><a href="#Ex7-17" class="headerlink" title="Ex7.17"></a>Ex7.17</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">不使用 <span class="keyword">public</span> 和 <span class="keyword">private</span> 的话， <span class="keyword">class</span> 后面的为 <span class="keyword">private</span> 成员，<span class="keyword">struct</span> 后面的为 <span class="keyword">public</span> 成员</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-18"><a href="#Ex7-18" class="headerlink" title="Ex7.18"></a>Ex7.18</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">封装就是只提供使用该类的接口，而不表明该类是如何实现的；</span><br><span class="line">封装使使用者更方便使该类</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-19"><a href="#Ex7-19" class="headerlink" title="Ex7.19"></a>Ex7.19</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">构造函数和 <span class="built_in">getName</span>()、<span class="built_in">getAddr</span>()应该为 <span class="keyword">public</span> 成员；</span><br><span class="line">变量 name 和 address 应该为 <span class="keyword">private</span> 成员</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-7-2-1"><a href="#Exercises-Section-7-2-1" class="headerlink" title="Exercises Section 7.2.1"></a>Exercises Section 7.2.1</h1><h2 id="Ex7-20"><a href="#Ex7-20" class="headerlink" title="Ex7.20"></a>Ex7.20</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">被声明为一个类的友元函数可以调用该类的 <span class="keyword">private</span> 成员；</span><br><span class="line">友元给函数提供了方便，但是也增加了函数的不安全性</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-21"><a href="#Ex7-21" class="headerlink" title="Ex7.21"></a>Ex7.21</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Sales_data</span> </span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">Sales_data</span>() = <span class="keyword">default</span>;</span><br><span class="line">    <span class="built_in">Sales_data</span>(<span class="type">const</span> std::string &amp;s) : <span class="built_in">bookNo</span>(s) &#123;&#125;</span><br><span class="line">    <span class="built_in">Sales_data</span>(<span class="type">const</span> std::string &amp;s, <span class="type">unsigned</span> n, <span class="type">double</span> p) : <span class="built_in">bookNo</span>(s), <span class="built_in">units_sold</span>(n), <span class="built_in">revenue</span>(p * n) &#123;&#125;</span><br><span class="line">    <span class="built_in">Sales_data</span>(std::istream &amp;);</span><br><span class="line">    <span class="function">std::string <span class="title">isbn</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> bookNo; &#125;</span><br><span class="line">    <span class="function">Sales_data &amp;<span class="title">combine</span><span class="params">( <span class="type">const</span> Sales_data &amp;)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    std::string bookNo;</span><br><span class="line">    <span class="type">unsigned</span> units_sold = <span class="number">0</span>;</span><br><span class="line">    <span class="type">double</span> revenue = <span class="number">0.0</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function">Sales_data&amp; <span class="title">Sales_data::combine</span><span class="params">(<span class="type">const</span> Sales_data &amp;rhs)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    units_sold += rhs.units_sold;</span><br><span class="line">    revenue += rhs.revenue;</span><br><span class="line">    <span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-22"><a href="#Ex7-22" class="headerlink" title="Ex7.22"></a>Ex7.22</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Person</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">Person</span>() = <span class="keyword">default</span>;</span><br><span class="line">    <span class="built_in">Person</span>(<span class="type">const</span> std::string &amp;s) : <span class="built_in">name</span>(s) &#123;&#125;</span><br><span class="line">    <span class="built_in">Person</span>(<span class="type">const</span> std::string &amp;s, <span class="type">const</span> std::string &amp;a) : <span class="built_in">name</span>(s), <span class="built_in">address</span>(a) &#123;&#125;</span><br><span class="line">    <span class="function">string <span class="title">getName</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> name; &#125;</span><br><span class="line">    <span class="function">string <span class="title">getAddr</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> address; &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    std::string name;</span><br><span class="line">    std::string address;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-7-3-1"><a href="#Exercises-Section-7-3-1" class="headerlink" title="Exercises Section 7.3.1"></a>Exercises Section 7.3.1</h1><h2 id="Ex7-23-7-24"><a href="#Ex7-23-7-24" class="headerlink" title="Ex7.23 7.24"></a>Ex7.23 7.24</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Screen</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">typedef</span> std::string::size_type pos;</span><br><span class="line">    <span class="built_in">Screen</span>() = <span class="keyword">default</span>;</span><br><span class="line">    <span class="built_in">Screen</span>(pos ht, pos wd, <span class="type">char</span> c) : <span class="built_in">height</span>(ht), <span class="built_in">width</span>(wd), <span class="built_in">contents</span>(ht * wd, c) &#123;&#125;</span><br><span class="line">    <span class="built_in">Screen</span>(pos ht, pos wd, <span class="type">char</span> c) : <span class="built_in">heiget</span>(ht), <span class="built_in">width</span>(wd), <span class="built_in">cursor</span>(c) &#123;&#125;</span><br><span class="line">    <span class="function"><span class="type">char</span> <span class="title">get</span><span class="params">()</span> <span class="type">const</span> </span>&#123;  <span class="keyword">return</span> contents[curosr]; &#125;</span><br><span class="line">    <span class="function"><span class="keyword">inline</span> <span class="type">char</span> <span class="title">get</span><span class="params">(pos ht, pos wd)</span> <span class="type">const</span></span>;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    pos cursor = <span class="number">0</span>;</span><br><span class="line">    pos height = <span class="number">0</span>, width = <span class="number">0</span>;</span><br><span class="line">    std::string contents;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-25"><a href="#Ex7-25" class="headerlink" title="Ex7.25"></a>Ex7.25</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">如果未显式定义带参构造函数，那么默认构造函数就是安全的。</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-26"><a href="#Ex7-26" class="headerlink" title="Ex7.26"></a>Ex7.26</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Sales_data</span> </span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">Sales_data</span>() = <span class="keyword">default</span>;</span><br><span class="line">    <span class="built_in">Sales_data</span>(<span class="type">const</span> std::string &amp;s) : <span class="built_in">bookNo</span>(s) &#123;&#125;</span><br><span class="line">    <span class="built_in">Sales_data</span>(<span class="type">const</span> std::string &amp;s, <span class="type">unsigned</span> n, <span class="type">double</span> p) : <span class="built_in">bookNo</span>(s), <span class="built_in">units_sold</span>(n), <span class="built_in">revenue</span>(p * n) &#123;&#125;</span><br><span class="line">    <span class="built_in">Sales_data</span>(std::istream &amp;);</span><br><span class="line">    <span class="function">std::string <span class="title">isbn</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> bookNo; &#125;</span><br><span class="line">    <span class="function">Sales_data &amp;<span class="title">combine</span><span class="params">( <span class="type">const</span> Sales_data &amp;)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="function"><span class="keyword">inline</span> <span class="type">double</span> <span class="title">avg_price</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> units_sold ? revenue / units_sold : <span class="number">0</span>; &#125;</span><br><span class="line">    std::string bookNo;</span><br><span class="line">    <span class="type">unsigned</span> units_sold = <span class="number">0</span>;</span><br><span class="line">    <span class="type">double</span> revenue = <span class="number">0.0</span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-7-3-2"><a href="#Exercises-Section-7-3-2" class="headerlink" title="Exercises Section 7.3.2"></a>Exercises Section 7.3.2</h1><h2 id="Ex7-27"><a href="#Ex7-27" class="headerlink" title="Ex7.27"></a>Ex7.27</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;string&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Screen</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">typedef</span> std::string::size_type pos;</span><br><span class="line">    <span class="built_in">Screen</span>() = <span class="keyword">default</span>;</span><br><span class="line">    <span class="built_in">Screen</span>(pos ht, pos wd, <span class="type">char</span> c) : <span class="built_in">height</span>(ht), <span class="built_in">width</span>(wd), <span class="built_in">contents</span>(ht * wd, c) &#123;&#125;</span><br><span class="line">    <span class="function"><span class="type">char</span> <span class="title">get</span><span class="params">()</span> <span class="type">const</span> </span>&#123;  <span class="keyword">return</span> contents[cursor]; &#125;</span><br><span class="line">    <span class="function"><span class="keyword">inline</span> <span class="type">char</span> <span class="title">get</span><span class="params">(pos ht, pos wd)</span> <span class="type">const</span></span>;</span><br><span class="line">    <span class="function">Screen &amp;<span class="title">move</span><span class="params">(pos r, pos c)</span></span>;</span><br><span class="line">    <span class="function">Screen &amp;<span class="title">set</span><span class="params">(<span class="type">char</span>)</span></span>;</span><br><span class="line">    <span class="function">Screen &amp;<span class="title">set</span><span class="params">(pos, pos, <span class="type">char</span>)</span></span>;</span><br><span class="line">    <span class="function">Screen &amp;<span class="title">display</span><span class="params">(std::ostream &amp;os)</span> </span>&#123; <span class="built_in">do_display</span>(os); <span class="keyword">return</span> *<span class="keyword">this</span>; &#125;</span><br><span class="line">    <span class="function"><span class="type">const</span> Screen &amp;<span class="title">display</span><span class="params">(std::ostream &amp;os)</span> <span class="type">const</span> </span>&#123; <span class="built_in">do_display</span>(os); <span class="keyword">return</span> *<span class="keyword">this</span>; &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">do_display</span><span class="params">(std::ostream &amp;os)</span> <span class="type">const</span> </span>&#123; os &lt;&lt; contents; &#125;</span><br><span class="line">    pos cursor = <span class="number">0</span>;</span><br><span class="line">    pos height = <span class="number">0</span>, width = <span class="number">0</span>;</span><br><span class="line">    std::string contents;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">char</span> <span class="title">Screen::get</span><span class="params">(pos r, pos c)</span> <span class="type">const</span> </span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    pos row = r * width;</span><br><span class="line">    <span class="keyword">return</span> contents[row + c];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">inline</span> Screen&amp; <span class="title">Screen::move</span><span class="params">(pos r, pos c)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    pos row = r * width;</span><br><span class="line">    cursor = row + c;</span><br><span class="line">    <span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">inline</span> Screen &amp;<span class="title">Screen::set</span><span class="params">(<span class="type">char</span> c)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    contents[cursor] = c;</span><br><span class="line">    <span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">inline</span> Screen &amp;<span class="title">Screen::set</span><span class="params">(pos r, pos col, <span class="type">char</span> ch)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    contents[r * width + col] = ch;</span><br><span class="line">    <span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="function">Screen <span class="title">myScreen</span><span class="params">(<span class="number">5</span>, <span class="number">5</span>, <span class="string">&#x27;X&#x27;</span>)</span></span>;</span><br><span class="line">    myScreen.<span class="built_in">move</span>(<span class="number">4</span>, <span class="number">0</span>).<span class="built_in">set</span>(<span class="string">&#x27;#&#x27;</span>).<span class="built_in">display</span>(cout);</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;\n&quot;</span>;</span><br><span class="line">    myScreen.<span class="built_in">display</span>(cout);</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;\n&quot;</span>;</span><br><span class="line">    <span class="built_in">system</span>(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/20210527165630547.png#pic_center" alt="在这里插入图片描述"></p>
<h2 id="Ex7-28"><a href="#Ex7-28" class="headerlink" title="Ex7.28"></a>Ex7.28</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">只会对其副本进行修改，不会改变对象的值。</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-29"><a href="#Ex7-29" class="headerlink" title="Ex7.29"></a>Ex7.29</h2><p><img src="https://img-blog.csdnimg.cn/20210527165642389.png#pic_center" alt="在这里插入图片描述"></p>
<h2 id="Ex7-30"><a href="#Ex7-30" class="headerlink" title="Ex7.30"></a>Ex7.30</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">优点：比较明确</span><br><span class="line">缺点：不够简洁</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-7-3-3"><a href="#Exercises-Section-7-3-3" class="headerlink" title="Exercises Section 7.3.3"></a>Exercises Section 7.3.3</h1><h2 id="Ex7-31"><a href="#Ex7-31" class="headerlink" title="Ex7.31"></a>Ex7.31</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">X</span></span><br><span class="line">&#123;</span><br><span class="line">    Y *y;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Y</span></span><br><span class="line">&#123;</span><br><span class="line">    X x;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-7-3-4"><a href="#Exercises-Section-7-3-4" class="headerlink" title="Exercises Section 7.3.4"></a>Exercises Section 7.3.4</h1><h2 id="Ex7-32"><a href="#Ex7-32" class="headerlink" title="Ex7.32"></a>Ex7.32</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Screen</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">friend</span> <span class="keyword">class</span> <span class="title class_">Window_mgr</span>;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">typedef</span> std::string::size_type pos;</span><br><span class="line">    <span class="built_in">Screen</span>() = <span class="keyword">default</span>;</span><br><span class="line">    <span class="built_in">Screen</span>(pos ht, pos wd, <span class="type">char</span> c) : <span class="built_in">height</span>(ht), <span class="built_in">width</span>(wd), <span class="built_in">contents</span>(ht * wd, c) &#123;&#125;</span><br><span class="line">    <span class="function"><span class="type">char</span> <span class="title">get</span><span class="params">()</span> <span class="type">const</span> </span>&#123;  <span class="keyword">return</span> contents[cursor]; &#125;</span><br><span class="line">    <span class="function"><span class="keyword">inline</span> <span class="type">char</span> <span class="title">get</span><span class="params">(pos ht, pos wd)</span> <span class="type">const</span></span>;</span><br><span class="line">    <span class="function">Screen &amp;<span class="title">move</span><span class="params">(pos r, pos c)</span></span>;</span><br><span class="line">    <span class="function">Screen &amp;<span class="title">set</span><span class="params">(<span class="type">char</span>)</span></span>;</span><br><span class="line">    <span class="function">Screen &amp;<span class="title">set</span><span class="params">(pos, pos, <span class="type">char</span>)</span></span>;</span><br><span class="line">    <span class="function">Screen &amp;<span class="title">display</span><span class="params">(std::ostream &amp;os)</span> </span>&#123; <span class="built_in">do_display</span>(os); <span class="keyword">return</span> *<span class="keyword">this</span>; &#125;</span><br><span class="line">    <span class="function"><span class="type">const</span> Screen <span class="title">display</span><span class="params">(std::ostream &amp;os)</span> <span class="type">const</span> </span>&#123; <span class="built_in">do_display</span>(os); <span class="keyword">return</span> *<span class="keyword">this</span>; &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">do_display</span><span class="params">(std::ostream &amp;os)</span> <span class="type">const</span> </span>&#123; os &lt;&lt; contents; &#125;</span><br><span class="line">    pos cursor = <span class="number">0</span>;</span><br><span class="line">    pos height = <span class="number">0</span>, width = <span class="number">0</span>;</span><br><span class="line">    std::string contents;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">char</span> <span class="title">Screen::get</span><span class="params">(pos r, pos c)</span> <span class="type">const</span> </span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    pos row = r * width;</span><br><span class="line">    <span class="keyword">return</span> contents[row + c];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">inline</span> Screen &amp;<span class="title">Screen::move</span><span class="params">(pos r, pos c)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    pos row = r * width;</span><br><span class="line">    cursor = row + c;</span><br><span class="line">    <span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">inline</span> Screen &amp;<span class="title">Screen::set</span><span class="params">(<span class="type">char</span> c)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    contents[cursor] = c;</span><br><span class="line">    <span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">inline</span> Screen &amp;<span class="title">Screen::set</span><span class="params">(pos r, pos col, <span class="type">char</span> ch)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    contents[r * width + col] = ch;</span><br><span class="line">    <span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Window_mgr</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">using</span> ScreenIndex = std::vector&lt;Screen&gt;::size_type;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">clear</span><span class="params">(ScreenIndex)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    std::vector&lt;Screen&gt; screens&#123;<span class="built_in">Screen</span>(<span class="number">24</span>, <span class="number">80</span>, <span class="string">&#x27; &#x27;</span>)&#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">Window_mgr::clear</span><span class="params">(ScreenIndex i)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Screen &amp;s = screens[i];</span><br><span class="line">    s.contents = <span class="built_in">string</span>(s.height * s.width, <span class="string">&#x27; &#x27;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-7-4"><a href="#Exercises-Section-7-4" class="headerlink" title="Exercises Section 7.4"></a>Exercises Section 7.4</h1><h2 id="Ex7-33"><a href="#Ex7-33" class="headerlink" title="Ex7.33"></a>Ex7.33</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">pos <span class="title">Screen::size</span><span class="params">()</span> <span class="type">const</span> 	<span class="comment">// pos 定义在类内，在类外找不到该类型</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">return</span> height * width;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 修改如下</span></span><br><span class="line"><span class="function">Screen::pos <span class="title">Screen::size</span><span class="params">()</span> <span class="type">const</span> </span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">return</span> height * width;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-7-4-1"><a href="#Exercises-Section-7-4-1" class="headerlink" title="Exercises Section 7.4.1"></a>Exercises Section 7.4.1</h1><h2 id="Ex7-34"><a href="#Ex7-34" class="headerlink" title="Ex7.34"></a>Ex7.34</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">当把 pos 类型声明放到最后，类会出错；前面定义的 pos 类的函数和变量都找不到该类。</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-35"><a href="#Ex7-35" class="headerlink" title="Ex7.35"></a>Ex7.35</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> string Type;</span><br><span class="line"><span class="function">Type <span class="title">initVal</span><span class="params">()</span></span>;     <span class="comment">// Type = string</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Exercise</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">typedef</span> <span class="type">double</span> Type;</span><br><span class="line">    <span class="function">Type <span class="title">setVal</span><span class="params">(Type)</span></span>;  <span class="comment">// Tpye = double</span></span><br><span class="line">    <span class="function">Type <span class="title">initVal</span><span class="params">()</span></span>;     <span class="comment">// Type = double</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="type">int</span> val;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="function">Type <span class="title">Exercise::setVal</span><span class="params">(Type parm)</span>    <span class="comment">// Type = string</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    val = parm + <span class="built_in">initVal</span>();</span><br><span class="line">    <span class="keyword">return</span> val;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-7-5-1"><a href="#Exercises-Section-7-5-1" class="headerlink" title="Exercises Section 7.5.1"></a>Exercises Section 7.5.1</h1><h2 id="Ex7-36"><a href="#Ex7-36" class="headerlink" title="Ex7.36"></a>Ex7.36</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">X</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">// base % j 无效</span></span><br><span class="line">    <span class="built_in">X</span> (<span class="type">int</span> i, <span class="type">int</span> j) : <span class="built_in">base</span>(i), <span class="built_in">rem</span>(base % j) &#123;&#125;</span><br><span class="line">    <span class="type">int</span> rem, base;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 修改如下</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">X</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">X</span> (<span class="type">int</span> i, <span class="type">int</span> j) : <span class="built_in">base</span>(i), <span class="built_in">rem</span>(i % j) &#123;&#125;</span><br><span class="line">    <span class="type">int</span> rem, base;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-37"><a href="#Ex7-37" class="headerlink" title="Ex7.37"></a>Ex7.37</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">Sales_data <span class="title">first_item</span><span class="params">(cin)</span></span>;		<span class="comment">// 参数为 std::istream &amp; 的构造函数</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	Sales_data next;	<span class="comment">// 默认构造函数</span></span><br><span class="line">    <span class="function">Sales_data <span class="title">last</span><span class="params">(<span class="string">&quot;9-999-99999-9&quot;</span>)</span></span>;	<span class="comment">// 参数为 std::string 的构造函数</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-38"><a href="#Ex7-38" class="headerlink" title="Ex7.38"></a>Ex7.38</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Sales_data</span>(std::istream &amp;is = cin);</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-39"><a href="#Ex7-39" class="headerlink" title="Ex7.39"></a>Ex7.39</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">不合法；产生二义性</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-40"><a href="#Ex7-40" class="headerlink" title="Ex7.40"></a>Ex7.40</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Book</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">Book</span>() = <span class="keyword">default</span>;</span><br><span class="line">    <span class="built_in">Book</span>(<span class="type">const</span> std::string s) : <span class="built_in">bookNo</span>(s) &#123;&#125;</span><br><span class="line">    <span class="built_in">Book</span>(<span class="type">const</span> std::string s, <span class="type">const</span> std::String n, <span class="type">const</span> <span class="type">double</span> p) : <span class="built_in">BookNo</span>(s), <span class="built_in">author_name</span>(n), <span class="built_in">price</span>(p) &#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    std::string bookNo;</span><br><span class="line">    std::string author_name;</span><br><span class="line">    <span class="type">double</span> price = <span class="number">0.0</span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-7-5-2"><a href="#Exercises-Section-7-5-2" class="headerlink" title="Exercises Section 7.5.2"></a>Exercises Section 7.5.2</h1><h2 id="Ex7-41"><a href="#Ex7-41" class="headerlink" title="Ex7.41"></a>Ex7.41</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Sales_data</span> </span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">Sales_data</span>(<span class="type">const</span> std::string &amp;s, <span class="type">unsigned</span> n, <span class="type">double</span> p) : <span class="built_in">bookNo</span>(s), <span class="built_in">units_sold</span>(n), <span class="built_in">revenue</span>(p * n) &#123;&#125;</span><br><span class="line">    <span class="built_in">Sales_data</span>(std::istream &amp;) : <span class="built_in">Sales_data</span>(<span class="string">&quot;&quot;</span>, <span class="number">0</span>, <span class="number">0</span>) &#123;&#125;</span><br><span class="line">    <span class="built_in">Sales_data</span>(<span class="type">const</span> std::string &amp;s) : <span class="built_in">Sales_data</span>(s, <span class="number">0</span>, <span class="number">0</span>) &#123;&#125;</span><br><span class="line">    <span class="built_in">Sales_data</span>(std::istream &amp;is) : <span class="built_in">Sales_data</span>(<span class="string">&quot;&quot;</span>, <span class="number">0</span>, <span class="number">0</span>) &#123; <span class="built_in">read</span>(is, *<span class="keyword">this</span>); &#125;</span><br><span class="line">    <span class="comment">// 其他成员一样</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-7-5-3"><a href="#Exercises-Section-7-5-3" class="headerlink" title="Exercises Section 7.5.3"></a>Exercises Section 7.5.3</h1><h2 id="Ex7-43"><a href="#Ex7-43" class="headerlink" title="Ex7.43"></a>Ex7.43</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">NoDefault</span> </span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">NoDefault</span>(<span class="type">const</span> <span class="type">int</span>) &#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">C</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">C</span>() : <span class="built_in">my_c</span>(<span class="number">0</span>) &#123;&#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    NoDefault my_c;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-44"><a href="#Ex7-44" class="headerlink" title="Ex7.44"></a>Ex7.44</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">vector&lt;NoDefault&gt; <span class="title">vec</span><span class="params">(<span class="number">10</span>)</span></span>;	<span class="comment">// 不合法，因为 NoDefault 中没有默认构造函数</span></span><br></pre></td></tr></table></figure>

<h2 id="Ex7-45"><a href="#Ex7-45" class="headerlink" title="Ex7.45"></a>Ex7.45</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">vector&lt;C&gt; <span class="title">vec</span><span class="params">(<span class="number">10</span>)</span></span>; <span class="comment">// 合法，因为 C 中有默认构造函数</span></span><br></pre></td></tr></table></figure>

<h2 id="Ex7-46"><a href="#Ex7-46" class="headerlink" title="Ex7.46"></a>Ex7.46</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">a) 不正确；不提供构造函数的话编译器会提供默认构造函数</span><br><span class="line">b) 不正确；默认构造函数不仅没有参数而且函数体中没有语句</span><br><span class="line">c) 不正确；编译器提供默认构造函数不关心值是否有意义</span><br><span class="line">d) 不正确；只有当没有显式提供任何构造函数时，编译器才会提供默认构造函数</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-7-5-4"><a href="#Exercises-Section-7-5-4" class="headerlink" title="Exercises Section 7.5.4"></a>Exercises Section 7.5.4</h1><h2 id="Ex7-47"><a href="#Ex7-47" class="headerlink" title="Ex7.47"></a>Ex7.47</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">防止构造函数进行隐式转换；</span><br><span class="line">优点：可以减少一些难以看懂的代码产生</span><br><span class="line">缺点：减少了能通过编译的代码</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-48"><a href="#Ex7-48" class="headerlink" title="Ex7.48"></a>Ex7.48</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 非explicit；调用构造函数进行隐式转换</span></span><br><span class="line"><span class="comment">// explicit；发生错误</span></span><br><span class="line"><span class="function">string <span class="title">null_isbn</span><span class="params">(<span class="string">&quot;9-999-99999-9&quot;</span>)</span></span>;</span><br><span class="line"><span class="function">Sales_data <span class="title">item1</span><span class="params">(null_isbn)</span></span>;	</span><br><span class="line"><span class="function">Sales_data <span class="title">item2</span><span class="params">(<span class="string">&quot;9-999-99999-9&quot;</span>)</span></span>;</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-49"><a href="#Ex7-49" class="headerlink" title="Ex7.49"></a>Ex7.49</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 调用 i.combine(s); i 为 Sales_data 类型， s 为 string 类型</span></span><br><span class="line">a) <span class="function">Sales_data &amp;<span class="title">combine</span><span class="params">(Sales_data)</span></span>;	<span class="comment">// 合法</span></span><br><span class="line">b) <span class="function">Sales_data &amp;<span class="title">combine</span><span class="params">(Sales_data&amp;)</span></span>;	<span class="comment">// 非法；std::string&amp; 类型不能转换为 Sales_data 类型</span></span><br><span class="line">c) <span class="function">Sales_data &amp;<span class="title">combine</span><span class="params">(<span class="type">const</span> Sales_data&amp;)</span> <span class="type">const</span></span>;	<span class="comment">// 非法；函数后面的 const 表明不能对函数体内成员进行修改</span></span><br></pre></td></tr></table></figure>

<h2 id="Ex7-50"><a href="#Ex7-50" class="headerlink" title="Ex7.50"></a>Ex7.50</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">Person</span> </span><br><span class="line">&#123;</span><br><span class="line">    <span class="function"><span class="keyword">friend</span> std::istream &amp;<span class="title">read</span><span class="params">(std::istream &amp;is, Person &amp;person)</span></span>;</span><br><span class="line">    <span class="function"><span class="keyword">friend</span> std::ostream &amp;<span class="title">print</span><span class="params">(std::ostream &amp;os, <span class="type">const</span> Person &amp;person)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">Person</span>() = <span class="keyword">default</span>;</span><br><span class="line">    <span class="built_in">Person</span>(<span class="type">const</span> std::string sname, <span class="type">const</span> std::string saddr) : <span class="built_in">name</span>(sname), <span class="built_in">address</span>(saddr) &#123; &#125;</span><br><span class="line">    <span class="function"><span class="keyword">explicit</span> <span class="title">Person</span><span class="params">(std::istream &amp;is)</span> </span>&#123; <span class="built_in">read</span>(is, *<span class="keyword">this</span>); &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function">std::string <span class="title">getName</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> name; &#125;</span><br><span class="line">    <span class="function">std::string <span class="title">getAddress</span><span class="params">()</span> <span class="type">const</span> </span>&#123; <span class="keyword">return</span> address; &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    std::string name;</span><br><span class="line">    std::string address;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function">std::istream &amp;<span class="title">read</span><span class="params">(std::istream &amp;is, Person &amp;person)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    is &gt;&gt; person.name &gt;&gt; person.address;</span><br><span class="line">    <span class="keyword">return</span> is;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">std::ostream &amp;<span class="title">print</span><span class="params">(std::ostream &amp;os, <span class="type">const</span> Person &amp;person)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    os &lt;&lt; person.name &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; person.address;</span><br><span class="line">    <span class="keyword">return</span> os;<span class="comment">//返回类型为引用</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-51"><a href="#Ex7-51" class="headerlink" title="Ex7.51"></a>Ex7.51</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">vector的单参数构造函数声明为 <span class="keyword">explicit</span> 避免了一些不必的隐式转换；</span><br><span class="line">string 的单参数构造函数不声明为 <span class="keyword">explicit</span> 可以方便使用者</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-7-5-5"><a href="#Exercises-Section-7-5-5" class="headerlink" title="Exercises Section 7.5.5"></a>Exercises Section 7.5.5</h1><h2 id="Ex7-52"><a href="#Ex7-52" class="headerlink" title="Ex7.52"></a>Ex7.52</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 聚合类需要保证类内的成员都无初始值</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Sales_data</span></span><br><span class="line">&#123;</span><br><span class="line">	std::string bookNo;</span><br><span class="line">    <span class="type">unsigned</span> units_sold;</span><br><span class="line">    <span class="type">double</span> revenue;</span><br><span class="line">&#125;;</span><br><span class="line">Sales_data item = &#123;<span class="string">&quot;978-0590353403&quot;</span>, <span class="number">25</span>, <span class="number">15.99</span>&#125;;</span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-7-5-6"><a href="#Exercises-Section-7-5-6" class="headerlink" title="Exercises Section 7.5.6"></a>Exercises Section 7.5.6</h1><h2 id="Ex7-53"><a href="#Ex7-53" class="headerlink" title="Ex7.53"></a>Ex7.53</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Debug</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="function"><span class="keyword">constexpr</span> <span class="title">Debug</span><span class="params">(<span class="type">bool</span> b = <span class="literal">true</span>)</span> : hw(b), io(b), other(b) &#123;</span>&#125;</span><br><span class="line">    <span class="function"><span class="keyword">constexpr</span> <span class="title">Debug</span><span class="params">(<span class="type">bool</span> h, <span class="type">bool</span> i, <span class="type">bool</span> o)</span> : hw(h), io(i), other(o) &#123;</span>&#125;</span><br><span class="line">    <span class="function"><span class="keyword">constexpr</span> <span class="type">bool</span> <span class="title">any</span><span class="params">()</span> </span>&#123; <span class="keyword">return</span> hw || io || other; &#125;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">set_io</span><span class="params">(<span class="type">bool</span> b)</span> </span>&#123; io = b; &#125;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">set_hw</span><span class="params">(<span class="type">bool</span> b)</span> </span>&#123; hw = b; &#125;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">set_other</span><span class="params">(<span class="type">bool</span> b)</span> </span>&#123; hw = b; &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">	<span class="type">bool</span> hw;</span><br><span class="line">    <span class="type">bool</span> io;</span><br><span class="line">    <span class="type">bool</span> other;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-54"><a href="#Ex7-54" class="headerlink" title="Ex7.54"></a>Ex7.54</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">不应该；声明为 <span class="keyword">constexpr</span> 的函数应该包含一条 <span class="keyword">return</span> 语句</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-55"><a href="#Ex7-55" class="headerlink" title="Ex7.55"></a>Ex7.55</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">是的； a aggregate <span class="keyword">class</span> <span class="title class_">must</span> be a literal <span class="keyword">class</span> </span><br></pre></td></tr></table></figure>

<h1 id="Exercises-Section-7-6"><a href="#Exercises-Section-7-6" class="headerlink" title="Exercises Section 7.6"></a>Exercises Section 7.6</h1><h2 id="Ex7-56"><a href="#Ex7-56" class="headerlink" title="Ex7.56"></a>Ex7.56</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> 成员与该类相关，而不是类内的对象；</span><br><span class="line">优点：每个 <span class="type">static</span> 成员都无需存储公共数据，而且更改了数据，每个成员都能使用新值；</span><br><span class="line"><span class="type">static</span> 成员可以声明为它所属的类型；而非 <span class="type">static</span> 成员只能声明为所属类</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-57"><a href="#Ex7-57" class="headerlink" title="Ex7.57"></a>Ex7.57</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Account</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">calculate</span><span class="params">()</span> </span>&#123; amount += amount * interestRate; &#125;</span><br><span class="line">    <span class="function"><span class="type">static</span> <span class="type">double</span> <span class="title">rate</span><span class="params">()</span> </span>&#123; <span class="keyword">return</span> interestRate; &#125;</span><br><span class="line">    <span class="function"><span class="type">static</span> <span class="type">void</span> <span class="title">rate</span><span class="params">(<span class="type">double</span>)</span></span>;</span><br><span class="line">    </span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    std::string owner;</span><br><span class="line">    <span class="type">double</span> amount;</span><br><span class="line">    <span class="type">static</span> <span class="type">double</span> interestRate;</span><br><span class="line">    <span class="function"><span class="type">static</span> <span class="type">double</span> <span class="title">initRate</span><span class="params">()</span></span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="Ex7-58"><a href="#Ex7-58" class="headerlink" title="Ex7.58"></a>Ex7.58</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// example.h</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Example</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="type">static</span> <span class="type">double</span> rate = <span class="number">6.5</span>;	<span class="comment">// 静态类内初始化必须是常量表达式</span></span><br><span class="line">    <span class="comment">// 修改： static const double rate = 6.5;</span></span><br><span class="line">    <span class="type">static</span> <span class="type">const</span> <span class="type">int</span> vecSize = <span class="number">20</span>;</span><br><span class="line">    <span class="function"><span class="type">static</span> vector&lt;<span class="type">double</span>&gt; <span class="title">vec</span><span class="params">(vecSize)</span></span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// example.c</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;example.h&quot;</span></span></span><br><span class="line"><span class="type">double</span> Example::rate;</span><br><span class="line">vector&lt;<span class="type">double</span>&gt; Example::vec;</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://astralcon.gitee.io/blog">astralcon</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://astralcon.gitee.io/blog/2022/05/14/Chapter-7-Classes/">https://astralcon.gitee.io/blog/2022/05/14/Chapter-7-Classes/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://astralcon.gitee.io/blog" target="_blank">A君的秘密基地</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blog/tags/C-Primer/">C++ Primer</a></div><div class="post_share"><div class="social-share" data-image="" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/gh/overtrue/share.js@master/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/blog/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" src="/blog/img/wechat.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/blog/img/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="/blog/img/alipay.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blog/2022/05/14/Chapter-8-The-I-O-Library/"><img class="prev-cover" src="" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Chapter 8: The I/O Library</div></div></a></div><div class="next-post pull-right"><a href="/blog/2022/05/14/Chapter-6-Functions/"><img class="next-cover" src="" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Chapter 6: Functions</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/blog/2022/05/14/Chapter-1-Getting-Started/" title="Chapter 1: Getting Started"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 1: Getting Started</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-10-Generic-Algorithms/" title="Chapter 10: Generic Algorithms"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 10: Generic Algorithms</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-11-Associative-Containers/" title="Chapter 11: Associative Containers"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 11: Associative Containers</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-12-Dynamic-Memory/" title="Chapter 12: Dynamic Memory"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 12: Dynamic Memory</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-13-Copy-Control/" title="Chapter 13: Copy Control"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 13: Copy Control</div></div></a></div><div><a href="/blog/2022/05/14/Chapter-15-Object-Oriented-Programming/" title="Chapter 15: Object-Oriented Programming"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-14</div><div class="title">Chapter 15: Object-Oriented Programming</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div><div id="comment-switch"><span class="first-comment">Valine</span><span class="switch-btn"></span><span class="second-comment">Disqus</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div><div><div id="disqus_thread"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/blog/img/avatar.jpg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">astralcon</div><div class="author-info__description">slow is fast.</div></div><div class="card-info-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">27</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">3</div></a></div><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/astralcon" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:a842181471@qq.com.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">朝九晚五，健康生活！</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-7-1-1"><span class="toc-number">1.</span> <span class="toc-text">Exercises Section 7.1.1</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-1"><span class="toc-number">1.1.</span> <span class="toc-text">Ex7.1</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-7-1-2"><span class="toc-number">2.</span> <span class="toc-text">Exercises Section 7.1.2</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-2"><span class="toc-number">2.1.</span> <span class="toc-text">Ex7.2</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-3"><span class="toc-number">2.2.</span> <span class="toc-text">Ex7.3</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-4"><span class="toc-number">2.3.</span> <span class="toc-text">Ex7.4</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-5"><span class="toc-number">2.4.</span> <span class="toc-text">Ex7.5</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-7-1-3"><span class="toc-number">3.</span> <span class="toc-text">Exercises Section 7.1.3</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-6"><span class="toc-number">3.1.</span> <span class="toc-text">Ex7.6</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-7"><span class="toc-number">3.2.</span> <span class="toc-text">Ex7.7</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-8"><span class="toc-number">3.3.</span> <span class="toc-text">Ex7.8</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-9"><span class="toc-number">3.4.</span> <span class="toc-text">Ex7.9</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-10"><span class="toc-number">3.5.</span> <span class="toc-text">Ex7.10</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-7-1-4"><span class="toc-number">4.</span> <span class="toc-text">Exercises Section 7.1.4</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-11-7-12-7-13-7-14"><span class="toc-number">4.1.</span> <span class="toc-text">Ex7.11 7.12 7.13 7.14</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-15"><span class="toc-number">4.2.</span> <span class="toc-text">Ex7.15</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-7-2"><span class="toc-number">5.</span> <span class="toc-text">Exercises Section 7.2</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-16"><span class="toc-number">5.1.</span> <span class="toc-text">Ex7.16</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-17"><span class="toc-number">5.2.</span> <span class="toc-text">Ex7.17</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-18"><span class="toc-number">5.3.</span> <span class="toc-text">Ex7.18</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-19"><span class="toc-number">5.4.</span> <span class="toc-text">Ex7.19</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-7-2-1"><span class="toc-number">6.</span> <span class="toc-text">Exercises Section 7.2.1</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-20"><span class="toc-number">6.1.</span> <span class="toc-text">Ex7.20</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-21"><span class="toc-number">6.2.</span> <span class="toc-text">Ex7.21</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-22"><span class="toc-number">6.3.</span> <span class="toc-text">Ex7.22</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-7-3-1"><span class="toc-number">7.</span> <span class="toc-text">Exercises Section 7.3.1</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-23-7-24"><span class="toc-number">7.1.</span> <span class="toc-text">Ex7.23 7.24</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-25"><span class="toc-number">7.2.</span> <span class="toc-text">Ex7.25</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-26"><span class="toc-number">7.3.</span> <span class="toc-text">Ex7.26</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-7-3-2"><span class="toc-number">8.</span> <span class="toc-text">Exercises Section 7.3.2</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-27"><span class="toc-number">8.1.</span> <span class="toc-text">Ex7.27</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-28"><span class="toc-number">8.2.</span> <span class="toc-text">Ex7.28</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-29"><span class="toc-number">8.3.</span> <span class="toc-text">Ex7.29</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-30"><span class="toc-number">8.4.</span> <span class="toc-text">Ex7.30</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-7-3-3"><span class="toc-number">9.</span> <span class="toc-text">Exercises Section 7.3.3</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-31"><span class="toc-number">9.1.</span> <span class="toc-text">Ex7.31</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-7-3-4"><span class="toc-number">10.</span> <span class="toc-text">Exercises Section 7.3.4</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-32"><span class="toc-number">10.1.</span> <span class="toc-text">Ex7.32</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-7-4"><span class="toc-number">11.</span> <span class="toc-text">Exercises Section 7.4</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-33"><span class="toc-number">11.1.</span> <span class="toc-text">Ex7.33</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-7-4-1"><span class="toc-number">12.</span> <span class="toc-text">Exercises Section 7.4.1</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-34"><span class="toc-number">12.1.</span> <span class="toc-text">Ex7.34</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-35"><span class="toc-number">12.2.</span> <span class="toc-text">Ex7.35</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-7-5-1"><span class="toc-number">13.</span> <span class="toc-text">Exercises Section 7.5.1</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-36"><span class="toc-number">13.1.</span> <span class="toc-text">Ex7.36</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-37"><span class="toc-number">13.2.</span> <span class="toc-text">Ex7.37</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-38"><span class="toc-number">13.3.</span> <span class="toc-text">Ex7.38</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-39"><span class="toc-number">13.4.</span> <span class="toc-text">Ex7.39</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-40"><span class="toc-number">13.5.</span> <span class="toc-text">Ex7.40</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-7-5-2"><span class="toc-number">14.</span> <span class="toc-text">Exercises Section 7.5.2</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-41"><span class="toc-number">14.1.</span> <span class="toc-text">Ex7.41</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-7-5-3"><span class="toc-number">15.</span> <span class="toc-text">Exercises Section 7.5.3</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-43"><span class="toc-number">15.1.</span> <span class="toc-text">Ex7.43</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-44"><span class="toc-number">15.2.</span> <span class="toc-text">Ex7.44</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-45"><span class="toc-number">15.3.</span> <span class="toc-text">Ex7.45</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-46"><span class="toc-number">15.4.</span> <span class="toc-text">Ex7.46</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-7-5-4"><span class="toc-number">16.</span> <span class="toc-text">Exercises Section 7.5.4</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-47"><span class="toc-number">16.1.</span> <span class="toc-text">Ex7.47</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-48"><span class="toc-number">16.2.</span> <span class="toc-text">Ex7.48</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-49"><span class="toc-number">16.3.</span> <span class="toc-text">Ex7.49</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-50"><span class="toc-number">16.4.</span> <span class="toc-text">Ex7.50</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-51"><span class="toc-number">16.5.</span> <span class="toc-text">Ex7.51</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-7-5-5"><span class="toc-number">17.</span> <span class="toc-text">Exercises Section 7.5.5</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-52"><span class="toc-number">17.1.</span> <span class="toc-text">Ex7.52</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-7-5-6"><span class="toc-number">18.</span> <span class="toc-text">Exercises Section 7.5.6</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-53"><span class="toc-number">18.1.</span> <span class="toc-text">Ex7.53</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-54"><span class="toc-number">18.2.</span> <span class="toc-text">Ex7.54</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-55"><span class="toc-number">18.3.</span> <span class="toc-text">Ex7.55</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Exercises-Section-7-6"><span class="toc-number">19.</span> <span class="toc-text">Exercises Section 7.6</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-56"><span class="toc-number">19.1.</span> <span class="toc-text">Ex7.56</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-57"><span class="toc-number">19.2.</span> <span class="toc-text">Ex7.57</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Ex7-58"><span class="toc-number">19.3.</span> <span class="toc-text">Ex7.58</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/17/%E3%80%8A%E7%AE%97%E6%B3%95%E8%AE%BE%E8%AE%A1%E6%89%8B%E5%86%8C%E3%80%8B%E9%9D%A2%E8%AF%95%E9%A2%98%EF%BC%9AChapter-1/" title="《算法设计手册》面试题：Chapter 1">《算法设计手册》面试题：Chapter 1</a><time datetime="2022-05-17T07:35:05.000Z" title="发表于 2022-05-17 15:35:05">2022-05-17</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/14/Wireshark-Lab-DHCP-v7-0/" title="Wireshark Lab: DHCP v7.0">Wireshark Lab: DHCP v7.0</a><time datetime="2022-05-14T07:19:53.000Z" title="发表于 2022-05-14 15:19:53">2022-05-14</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/14/Wireshark-Lab-Ethernet-and-ARP-v7-0/" title="Wireshark Lab: Ethernet and ARP v7.0">Wireshark Lab: Ethernet and ARP v7.0</a><time datetime="2022-05-14T07:18:10.000Z" title="发表于 2022-05-14 15:18:10">2022-05-14</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/14/Wireshark-Lab-ICMP-v7-0/" title="Wireshark Lab: ICMP v7.0">Wireshark Lab: ICMP v7.0</a><time datetime="2022-05-14T07:16:41.000Z" title="发表于 2022-05-14 15:16:41">2022-05-14</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/blog/2022/05/14/Wireshark-Lab-NAT-v7-0/" title="Wireshark Lab: NAT v7.0">Wireshark Lab: NAT v7.0</a><time datetime="2022-05-14T07:15:13.000Z" title="发表于 2022-05-14 15:15:13">2022-05-14</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 By astralcon</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="algolia-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="search-wrap"><div id="algolia-search-input"></div><hr/><div id="algolia-search-results"><div id="algolia-hits"></div><div id="algolia-pagination"></div><div id="algolia-info"><div class="algolia-stats"></div><div class="algolia-poweredBy"></div></div></div></div></div><div id="search-mask"></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="https://cdn.jsdelivr.net/npm/algoliasearch@4/dist/algoliasearch-lite.umd.js"></script><script src="https://cdn.jsdelivr.net/npm/instantsearch.js@4/dist/instantsearch.production.min.js"></script><script src="/blog/js/search/algolia.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: 'Uh3CYGE2t1Eaz4KoTkyVQv1N-gzGzoHsz',
      appKey: '1Gdg4rGlmT4Dcfv7oNiCH1q3',
      avatar: 'monsterid',
      serverURLs: '',
      emojiMaps: "",
      path: window.location.pathname,
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !false) {
  if (false) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script><script>function loadDisqus () {
  var disqus_config = function () {
    this.page.url = 'https://astralcon.gitee.io/blog/2022/05/14/Chapter-7-Classes/'
    this.page.identifier = '2022/05/14/Chapter-7-Classes/'
    this.page.title = 'Chapter 7: Classes'
  };

  window.disqusReset = () => {
    DISQUS.reset({
      reload: true,
      config: disqus_config
    })
  }

  if (window.DISQUS) disqusReset()
  else {
    (function() { 
      var d = document, s = d.createElement('script');
      s.src = 'https://.disqus.com/embed.js';
      s.setAttribute('data-timestamp', +new Date());
      (d.head || d.body).appendChild(s);
    })();
  }
}

if ('Valine' === 'Disqus' || !false) {
  if (false) btf.loadComment(document.getElementById('disqus_thread'), loadDisqus)
  else loadDisqus()
} else {
  function loadOtherComment () {
    loadDisqus()
  }
}
</script></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script src="/blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body></html>